import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NumberInput extends StatefulWidget {
  final Function(double) onChange;
  final double? width;
  final double? height;

  const NumberInput({Key? key, required this.onChange, this.width, this.height})
      : super(key: key);

  @override
  State<NumberInput> createState() => _NumberInputState();
}

class _NumberInputState extends State<NumberInput> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.height ?? 40,
      width: widget.width ?? 120,
      child: TextField(
        keyboardType: TextInputType.number,
        maxLines: 1,
        textAlign: TextAlign.center,
        style: const TextStyle(textBaseline: TextBaseline.alphabetic),
        decoration: InputDecoration(
          hintStyle: TextStyle(
              color: Color.fromRGBO(153, 151, 151, 0.3), fontSize: 16),
          hintText: "输入数值",
          // border: OutlineInputBorder(),
          alignLabelWithHint: true,
          contentPadding: EdgeInsets.only(top: 0, bottom: 0),
        ),
        onChanged: (val) {
          widget.onChange(double.tryParse(val) ?? 0);
        },
      ),
    );
  }
}
